home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / chkdaemon < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.9 KB  |  103 lines

  1. #!/bin/ksh
  2. # @(#) chkdaemon.ksh 1.0 97/07/18
  3. # 96/05/19 john h. dubois iii
  4. # 97/07/18 Improved help.
  5.  
  6. # This utility shouldn't be neccessary, but some daemons have no option to
  7. # not fork & run in the background, so they can't be run from inittab w/respawn
  8.  
  9. name=${0##*/}
  10. Usage="Usage: $name [-hvV] [-m<PID>] <daemon-name> <PID-file> ..."
  11. Verbose=false
  12. ExtraVerbose=false
  13. defMinPID=8
  14. typeset -i minPID=$defMinPID
  15. typeset -i pid
  16.  
  17. while getopts hm:vV opt; do
  18.     case $opt in
  19.     h)
  20.     echo \
  21. "$name: restart daemons which have died.
  22. <daemon-name> is the name of a process that should be running.
  23. <PID-file> is the name of a file that contains the process ID of the daemon
  24. as an ASCII number.  The file should contain nothing but the PID and optional
  25. whitespace.  A PID-file must be given for each daemon-name, so there must
  26. always be an even number of non-option arguments.
  27. For each pair of arguments, if the PID read from the file is no longer running,
  28. the associated daemon-name is run.  No check is done for whether the process
  29. name for the PID is the same as daemon-name; daemon-name is used only as the
  30. command to run to restart the process.  daemon-name should generally include a
  31. full path.  If options are given, the command name and options should be quoted
  32. so that they are passed to $name as a single argument.
  33. Example:
  34. $name -v /etc/named /etc/named.pid
  35. $Usage
  36. Options:
  37. -h: Print this help.
  38. -m<PID>: Set the minimum PID that will be believed to <PID>.  The default is
  39.     $defMinPID.  If the PID read from the PID-file is less than this, a
  40.     complaint will be printed and no other action will be taken.
  41. -v: Print a report to the standard output any time a daemon is restarted.
  42.     If $name is run from cron, cron will mail the report to the cron job
  43.     owner.
  44. -V: Print a report to the standard output telling whether or not each
  45.     daemon was restarted."
  46.     exit 0
  47.     ;;
  48.     m)
  49.     minPID=$OPTARG || exit 1
  50.     ;;
  51.     v)
  52.     Verbose=true
  53.     ;;
  54.     V)
  55.     ExtraVerbose=true
  56.     Verbose=true
  57.     ;;
  58.     ?)
  59.     print -u2 "Use -h for help."
  60.     exit 1
  61.     ;;
  62.     esac
  63. done
  64.  
  65. # remove args that were options
  66. let OPTIND=OPTIND-1
  67. shift $OPTIND
  68.  
  69. if [ $# -lt 2 ]; then
  70.     print -u2 "$Usage\nUse -h for help."
  71.     exit 1
  72. fi
  73. if [ $(( $# % 2 )) -ne 0 ]; then
  74.     print -u2 \
  75.     "$name: must give an even number of parameters.  Use -h for help."
  76.     exit 1
  77. fi
  78.  
  79. while [ $# -gt 0 ]; do
  80.     daemon=$1
  81.     pidFile=$2
  82.  
  83.     if [ ! -f "$pidFile" ]; then
  84.     $daemon &
  85.     $Verbose && print \
  86.     "$name: Restarted daemon (PID file $pidFile did not exist): $daemon"
  87.     elif pid=$(<$pidFile); then
  88.     if [ "$pid" -lt $minPID ]; then
  89.         print -u2 \
  90.         "$name: Bad PID for daemon $daemon read from PID file $pidFile"
  91.     elif ! kill -0 "$pid" 2>/dev/null; then
  92.         "$daemon" &
  93.         $Verbose && print \
  94.         "$name: Restarted daemon (dead PID read from PID file): $daemon"
  95.     else
  96.         $ExtraVerbose &&
  97.         print \
  98.         "$name: Did not restart daemon $daemon; still running (PID=$pid)"
  99.     fi
  100.     fi
  101.     shift 2
  102. done
  103.